package com.miyu.test.testUtils;



import org.lionsoul.ip2region.xdb.Searcher;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


/**
 * ip地址
 * 根据ip地址 查询ip归属
 * @author miyu
 * @date 2022/12/26
 */
public class IpAddress {
    public static void main(String[] args) throws Exception {
        //国内ip
        String ip1 = "192.168.128.140";

        String ipAddress = "IP地址";
//        JSONObject detail = getCityInfo(ipAddress);
//        System.out.println("输入的IP为：" + ipAddress + "，返回的结果为：" +detail);
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"ip2region.xdb";
        System.out.println(path);

        // 1、创建 searcher 对象
        String dbPath = path;
        Searcher searcher = null;
        try {
            searcher = Searcher.newWithFileOnly(dbPath);
        } catch (IOException e) {
            System.out.println(">>"+dbPath+ e);
            return;
        }

        // 2、查询
        try {
            String ip = "106.13.229.210";
            long sTime = System.nanoTime();
            String region = searcher.search(ip);
            long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
            System.out.println( region+":"+ searcher.getIOCount()+":"+cost);
        } catch (Exception e) {
            System.out.println("failed to search(%s): %s");
        }

        // 3、关闭资源
        searcher.close();

        // 备注：并发使用，每个线程需要创建一个独立的 searcher 对象单独使用。

    }


}
